package vsvm.gui.forms;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import vsvm.classifier.AbstractClassifier;
import vsvm.classifier.LibSvmClassifier;
//import vsvm.classifier.MultiLibSvmClassifier;
import vsvm.gui.Environment;
import vsvm.gui.objects.Workspace;

public class CreateClassifierDialog extends JDialog {
	private final static String LIBSVM = "MultiLIBSVM classifier";
	private final static String JSVM = "jSVM classifier";

	private JPanel jContentPane = null;
	private JPanel buttonsPanel = null;
	private JButton btnCreate = null;
	private JButton btnCancel = null;
	private JPanel centerPanel = null;
	private JLabel lblName = null;
	private JTextField txtName = null;
	private JLabel lblType = null;
	private JComboBox cmbTypes = null;
	
	private class ClassifierCreateAction extends AbstractAction {
		private CreateClassifierDialog dlg;
		
		public ClassifierCreateAction(CreateClassifierDialog d) {
			dlg = d;			
		}

		public void actionPerformed(ActionEvent e) {
			dlg.createClassifier();			
		}
	}
	
	private class ClassifierCancelAction extends AbstractAction {
		private CreateClassifierDialog dlg;
		
		public ClassifierCancelAction(CreateClassifierDialog d) {
			dlg = d;			
		}

		public void actionPerformed(ActionEvent e) {
			dlg.dispose();			
		}
	}

	
	public void createClassifier() {
		if (txtName.getText().trim().equals("")) {
			JOptionPane.showMessageDialog(this, "Please enter classifier name.", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		String sel = cmbTypes.getSelectedItem().toString();
		AbstractClassifier c = null;
		if (sel.equals(LIBSVM)) {
			c = new LibSvmClassifier();
		} else if (sel.equals(JSVM)) {
			c = new LibSvmClassifier();
		}
		
		Workspace wsp = Environment.getEnvironment().getWorkspace();
		c.setName(txtName.getText());
		wsp.addClassifier(c);	
		
		this.dispose();
		
		c.configure();
		
	}
	
	
	
	public CreateClassifierDialog() {
		super(Environment.getEnvironment().getMainFrame());
		initialize();
		setModal(true);
		this.setLocationRelativeTo(Environment.getEnvironment().getMainFrame());
	}
	
	/**
	 * This method initializes buttonsPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(25);
			buttonsPanel = new JPanel();
			buttonsPanel.setLayout(flowLayout);
			buttonsPanel.setPreferredSize(new java.awt.Dimension(10,40));
			buttonsPanel.add(getBtnCreate(), null);
			buttonsPanel.add(getBtnCancel(), null);
		}
		return buttonsPanel;
	}

	/**
	 * This method initializes btnCreate	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCreate() {
		if (btnCreate == null) {
			btnCreate = new JButton();
			btnCreate.setPreferredSize(new java.awt.Dimension(80,25));
			btnCreate.setAction(new ClassifierCreateAction(this));
			btnCreate.setText("Create");
		}
		return btnCreate;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setPreferredSize(new java.awt.Dimension(80,25));
			btnCancel.setAction(new ClassifierCancelAction(this));
			btnCancel.setText("Cancel");
		}
		return btnCancel;
	}

	/**
	 * This method initializes centerPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getCenterPanel() {
		if (centerPanel == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new java.awt.Insets(5,10,5,15);
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.insets = new java.awt.Insets(5,15,5,10);
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.gridy = 1;
			lblType = new JLabel();
			lblType.setText("Classifier type");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new java.awt.Insets(5,10,5,15);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new java.awt.Insets(5,15,5,10);
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridy = 0;
			lblName = new JLabel();
			lblName.setText("Classifier name");
			centerPanel = new JPanel();
			centerPanel.setLayout(new GridBagLayout());
			centerPanel.add(lblName, gridBagConstraints);
			centerPanel.add(getTxtName(), gridBagConstraints1);
			centerPanel.add(lblType, gridBagConstraints2);
			centerPanel.add(getCmbTypes(), gridBagConstraints3);
		}
		return centerPanel;
	}

	/**
	 * This method initializes txtName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtName() {
		if (txtName == null) {
			txtName = new JTextField();
		}
		return txtName;
	}

	/**
	 * This method initializes cmbTypes	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCmbTypes() {
		if (cmbTypes == null) {
			cmbTypes = new JComboBox();
			cmbTypes.addItem(LIBSVM);
			//cmbTypes.addItem(JSVM);
		}
		return cmbTypes;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//CreateDataSourceDialog d = new CreateDataSourceDialog();
		//d.setVisible(true);

	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(423, 173);
		this.setTitle("Create classifier");
		this.setResizable(false);
		this.setPreferredSize(new java.awt.Dimension(180,120));
		this.setName("CreateDataSourceDialog");
		this.setModal(true);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
		getRootPane().setDefaultButton(btnCreate);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonsPanel(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}

}
